package com.xinfu.newproject.ui.resident_certifition;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.ResidentCertificationActivity;
import com.xinfu.newproject.base.BaseFragment;
import com.xinfu.newproject.bean.certifition.ResidentBean;
import com.xinfu.newproject.bean.certifition.ResidentCertificationBean;
import com.xinfu.newproject.bean.certifition.UntyingBean;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.presenter.UntyingPresenter;
import com.xinfu.newproject.util.AlertDialog;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.SPUtils;
import com.xinfu.newproject.view.IView;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

import static com.xinfu.newproject.activity.ResidentCertificationActivity.FRAGMENT_ONE;
import static com.xinfu.newproject.activity.ResidentCertificationActivity.FRAGMENT_ZERO;
import static com.xinfu.newproject.contants.Constant.LOGIN;

public class CertifitionAuditFragment extends BaseFragment implements IView.IUntyingView {

    @BindView(R.id.head_text)
    TextView titleTv;

    @BindView(R.id.layout_progress_tv2)
    TextView oneTv2;

    @BindView(R.id.layout_progress_tv3)
    TextView oneTv3;

    @BindView(R.id.layout_progress_tv4)
    TextView oneTv4;

    @BindView(R.id.layout_progress_two_tv1)
    TextView twoTv1;

    @BindView(R.id.layout_progress_two_tv2)
    TextView twoTv2;

    @BindView(R.id.layout_progress_two_tv3)
    TextView twoTv3;

    @BindView(R.id.layout_progress_two_tv4)
    TextView twoTv4;

    @BindView(R.id.layout_progress_v1)
    View v1;

    @BindView(R.id.layout_progress_v2)
    View v2;

    @BindView(R.id.layout_progress_two_v2)
    View twoV2;

    @BindView(R.id.layout_progress_v3)
    View v3;

    @BindView(R.id.layout_progress_two_v3)
    View twoV3;

    @BindView(R.id.layout_progress_v4)
    View v4;

    @BindView(R.id.layout_progress_rl4)
    RelativeLayout progressRl4;

    @BindView(R.id.fragment_certifition_audit_name_tv)
    TextView nameTv;

    @BindView(R.id.fragment_certifition_audit_phone_tv)
    TextView phoneTv;

    @BindView(R.id.fragment_certifition_audit_address_tv)
    TextView addressTv;

    @BindView(R.id.fragment_certifition_audit_identity_tv)
    TextView idTv;

    @BindView(R.id.fragment_certifition_audit_status_tv)
    TextView statusTv;

    @BindView(R.id.fragment_certifition_audit_reason_tv)
    TextView reasonTv;

    @BindView(R.id.fragment_certifition_audit_reason_ll)
    LinearLayout reasonLL;

    @BindView(R.id.layout_next_step_rl)
    RelativeLayout nextRL;

    @BindView(R.id.layout_next_step_tv)
    TextView nextTv;

    private BackHandledInterface backHandledInterface;

    private UntyingPresenter untyingPresenter = new UntyingPresenter(this);

    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_certifition_audit;
    }

    @Override
    protected void setUpView() {
        titleTv.setText("我的认证");
        refresh();
    }

    private void initHeadProgeress(int type) {
        v4.setVisibility(View.GONE);
        twoV3.setVisibility(View.GONE);
        oneTv2.setText("2");
        oneTv2.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
        oneTv2.setBackgroundResource(R.drawable.custom_text_press);
        oneTv4.setVisibility(View.GONE);
        twoTv4.setVisibility(View.GONE);
        twoTv2.setTextColor(getActivity().getResources().getColor(R.color.location_color));
        v2.setBackgroundResource(R.color.yellow_ff);
        v1.setBackgroundResource(R.color.yellow_ff);
        progressRl4.setVisibility(View.GONE);
        twoTv1.setText("提交认证");
        twoTv2.setText("审核中");

        switch (type) {
            case 0:
                twoTv3.setText("认证成功");
                oneTv3.setText("3");
                oneTv3.setTextColor(getActivity().getResources().getColor(R.color.location_color));
                oneTv3.setBackgroundResource(R.drawable.custom_text_normal);
                twoV2.setBackgroundColor(getActivity().getResources().getColor(R.color.gray_ec));
                twoTv3.setTextColor(getActivity().getResources().getColor(R.color.gray_9));
                v3.setBackgroundColor(getActivity().getResources().getColor(R.color.gray_ec));
                nextRL.setVisibility(View.GONE);
                break;
            case 1:
                twoTv3.setText("认证成功");
                oneTv3.setText("√");
                oneTv3.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
                oneTv3.setBackgroundResource(R.drawable.custom_text_press);
                twoV2.setBackgroundColor(getActivity().getResources().getColor(R.color.yellow_ff));
                twoTv3.setTextColor(getActivity().getResources().getColor(R.color.location_color));
                v3.setBackgroundColor(getActivity().getResources().getColor(R.color.yellow_ff));
                nextRL.setVisibility(View.GONE);
                break;
            case -1:
                twoTv3.setText("已驳回");
                oneTv3.setText("!");
                oneTv3.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
                oneTv3.setBackgroundResource(R.drawable.custom_text_press);
                twoV2.setBackgroundColor(getActivity().getResources().getColor(R.color.yellow_ff));
                twoTv3.setTextColor(getActivity().getResources().getColor(R.color.location_color));
                v3.setBackgroundColor(getActivity().getResources().getColor(R.color.yellow_ff));
                nextRL.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected void setUpData() {

    }

    @OnClick({R.id.head_back, R.id.layout_next_step_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.head_back:
                backHandledInterface = (BackHandledInterface) getMContext();
                backHandledInterface.setSelectedFragment(FRAGMENT_ZERO);
                break;
            case R.id.layout_next_step_tv:
                if (nextTv.getText().toString().equals("解除房产")) {
                    AlertLog("是否解除房产?");
                } else if (nextTv.getText().toString().equals("重新提交认证")) {
                    backHandledInterface = (BackHandledInterface) getMContext();
                    backHandledInterface.setSelectedFragment(FRAGMENT_ONE);
                }
                break;
        }
    }

    private void AlertLog(String msg) {
        AlertDialog myDialog = new AlertDialog(getMContext()).builder();
        myDialog.setGone().setTitle(msg).setMsg("").setNegativeButton("取消", R.color.gray_a5, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myDialog.dismiss();
            }
        }).setPositiveButton("确定", R.color.blue_4a, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isLogin()) {
                    untyingPresenter.untying();
                }
            }
        }).show();
    }

    private void refresh() {
        Bundle bundle;
        if (getArguments() != null) {
            bundle = getArguments();
            String response = bundle.getString("response");
            ResidentBean residentBeans = new Gson().fromJson(response, ResidentBean.class);
            ResidentCertificationBean.ResultBean.RoomCheckUserListBean video = (ResidentCertificationBean.ResultBean.RoomCheckUserListBean) residentBeans.t;
            if (video.getApplyName() != null) {
                nameTv.setText(video.getApplyName());
                phoneTv.setText(video.getApplyTel());
                addressTv.setText(video.getAddress());
                switch (video.getIdType()) {
                    case 1:
                        idTv.setText("业主");
                        break;
                    case 2:
                        idTv.setText("租户");
                        break;
                    case 3:
                        idTv.setText("同住人");
                        break;
                }
                initHeadProgeress(video.getAuditRes());


                switch (video.getAuditRes()) {
                    case 0:
                        Drawable dra = getResources().getDrawable(R.mipmap.funnel_icon);
                        dra.setBounds(0, 0, dra.getMinimumWidth(), dra.getMinimumHeight());
                        statusTv.setText("审核中");
                        reasonLL.setVisibility(View.GONE);
                        statusTv.setCompoundDrawables(dra, null, null, null);
                        nextTv.setText("");
                        nextTv.setVisibility(View.GONE);
                        nextRL.setVisibility(View.GONE);
                        break;
                    case 1:
                        Drawable dra1 = getResources().getDrawable(R.mipmap.audit_pass_icon);
                        dra1.setBounds(0, 0, dra1.getMinimumWidth(), dra1.getMinimumHeight());
                        statusTv.setText("认证成功");
                        reasonLL.setVisibility(View.GONE);
                        statusTv.setCompoundDrawables(dra1, null, null, null);
                        nextTv.setText("解除房产");
                        nextTv.setVisibility(View.VISIBLE);
                        nextRL.setVisibility(View.VISIBLE);
                        break;
                    case -1:
                        Drawable dra2 = getResources().getDrawable(R.mipmap.audit_fail_icon);
                        dra2.setBounds(0, 0, dra2.getMinimumWidth(), dra2.getMinimumHeight());
                        statusTv.setText("已驳回");
                        reasonLL.setVisibility(View.VISIBLE);
                        reasonTv.setText(video.getAuditComment());
                        statusTv.setCompoundDrawables(dra2, null, null, null);
                        nextTv.setText("重新提交认证");
                        nextTv.setVisibility(View.VISIBLE);
                        nextRL.setVisibility(View.VISIBLE);
                        break;
                }
            }

        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (nameTv != null) {
                refresh();
            }
        }
    }

    /**
     * 判断是否可以请求数据
     */
    String token, userId, userName;

    private boolean isLogin() {
        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(getMContext(), LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
        } else {
            BToast.showText(getMContext(), "未登录");
            return false;
        }
        return true;
    }

    @Override
    public String header() {
        return token;
    }

    @Override
    public String id() {
        Bundle bundle = getArguments();
        String response = bundle.getString("response");
        ResidentBean residentBeans = new Gson().fromJson(response, ResidentBean.class);
        ResidentCertificationBean.ResultBean.RoomCheckUserListBean video = (ResidentCertificationBean.ResultBean.RoomCheckUserListBean) residentBeans.t;
        return video.getId();
    }

    @Override
    public void showToast(String content) {
    }

    @Override
    public void showProgress() {
        ((ResidentCertificationActivity) getMContext()).showLoding();
    }

    @Override
    public void hideProgress() {
        ((ResidentCertificationActivity) getMContext()).hideLoding();
    }

    @Override
    public void closeDispose(Disposable disposable) {

    }

    @Override
    public void getData(UntyingBean untyingBean) {
        BToast.showText(getMContext(), untyingBean.getMessage());
        backHandledInterface = (BackHandledInterface) getMContext();
        backHandledInterface.setSelectedFragment(FRAGMENT_ZERO);
    }
}
